import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:syria_sports/presentation/gyms/gyms_page/models/gym.dart';

import '../../../../core/utils/show_toast.dart';
import '../../../../data/controllers/sports_controller.dart';
import '../../sports_page/models/sport.dart';
import '../models/gym_sport.dart';

part 'sports_state.dart';
part 'sports_event.dart';

class GymSportsCubit extends Bloc<GymSportsEvent, GymSportsState> {
  GymSportsCubit(GymSportsState initialState) : super(SportsInitial()){
    on<GymSportsInitialEvent>(_onInitialize);
    on<GetGymSportsEvent>(_onGetSports);
    on<SelectModeEvent>(_onChangeSelectMode);
    on<SelectSportEvent>(_onSelectSport);
    on<AddSportToGymEvent>(_onAddGymToSport);
  }

  static GymSportsCubit get(context) => BlocProvider.of(context);
  List<Sport> sports = [];
  List<Sport> selectedSports = [];
  bool selectMode = false;
  late Gym gym;

  _onInitialize(
      GymSportsInitialEvent event,
      Emitter<GymSportsState> emit,
      )async {
    gym = event.gym;
    sports = [];
    emit(SportsLoading());
    await SportsController.getGymSports(gymId: gym.gymId).catchError((e){
      emit(SportsError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        add(SelectModeEvent(mode: false));
        sports = List<Sport>.generate(value.data.length, (index) => GymSport.fromJson(value.data[index]).sport);
        emit(SportsDone());
      }else if(value != null){
        showMessage(value.message);
        emit(SportsError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }


  _onGetSports(
      GetGymSportsEvent event,
      Emitter<GymSportsState> emit,
      )async{
    sports = [];
    emit(SportsLoading());
    await SportsController.getAllSports().catchError((e){
      emit(SportsError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        sports = List<Sport>.generate(value.data.length, (index) => Sport.fromJson(value.data[index]));
        emit(SportsDone());
      }else if(value != null){
        showMessage(value.message);
        emit(SportsError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }

  _onAddGymToSport(
      AddSportToGymEvent event,
      Emitter<GymSportsState> emit,
      )async{
    sports = [];
    emit(AddSportToGymLoading(sport: event.sport,gym:event.gym ));
    await SportsController.addToGym(sport: event.sport, gym: event.gym).catchError((e){
      emit(AddSportToGymError(
          sport: event.sport,
          gym:event.gym,
        massage: '',
        stateCode: -1,
      ));
      return null;
    }).then((value){
      if(value != null && value.state) {
        emit(AddSportToGymDone(sport: event.sport,gym:event.gym ,gymSport:GymSport.fromJson(value.data), ));
      }else if(value != null){
        showMessage(value.message);
        emit(AddSportToGymError(
          sport: event.sport,
          gym:event.gym,
          massage: value.message,
          stateCode: -1,
        ));
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }


  _onChangeSelectMode(
      SelectModeEvent event,
      Emitter<GymSportsState> emit,
      )async{
    selectMode = event.mode;
    if(event.sport != null){
      selectedSports.add(event.sport!);
    }
    if(!event.mode){
      selectedSports.clear();
    }
    emit(SelectModeUpdate( mode: selectMode));
  }

  _onSelectSport(
      SelectSportEvent event,
      Emitter<GymSportsState> emit,
      )async{
    if(event.isSelected){
      selectedSports.add(event.sport);
    }else{
      selectedSports.remove(event.sport);
    }
    emit(SelectSport(sport: event.sport,isSelected: event.isSelected));
  }


  Future onRefresh()async{
    add(GymSportsInitialEvent(gym: gym));
  }
}
